package com.ecnu.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ecnu.common.annotation.DataSource;
import com.ecnu.common.constant.HttpStatus;
import com.ecnu.common.core.page.PageDomain;
import com.ecnu.common.core.page.TableDataInfo;
import com.ecnu.common.enums.DataSourceType;
import com.ecnu.common.utils.OtherUtil;
import com.ecnu.common.utils.PageUtils;
import com.ecnu.system.domain.PublicDataApi;
import com.ecnu.system.domain.vo.PublicApiVO;
import com.ecnu.system.mapper.PublicDataApiMapper;
import com.ecnu.system.service.IPublicDataApiService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zdn
 * @since 2025-03-25
 */
@Service
@DataSource(value = DataSourceType.SLAVE)//  指定数据源
public class PublicDataApiServiceImpl extends ServiceImpl<PublicDataApiMapper, PublicDataApi> implements IPublicDataApiService {

    @Autowired
    private PublicDataApiMapper publicDataApiMapper;

    protected void startPage(PageDomain pageDomain) {
        PageUtils.startPage(pageDomain);
    }

    @Override
    public TableDataInfo getPublicData(PageDomain domain) {
        try {
            System.out.println("---获取学校活动信息---");
            StringBuilder builder = new StringBuilder();
            LocalDate now = LocalDate.now();
            QueryWrapper<PublicDataApi> wrapper = new QueryWrapper<>();
            wrapper.le("date(startDate)", now)
                    .ge("date(endDate)", now);
            wrapper.apply("meetingName REGEXP '研|司|课|班|赛|导|访|实|论文|讲座|本科|组会'");

            // 添加 meetingName 不包含特定关键词的条件
            wrapper.apply("meetingName NOT REGEXP '辅导员|论文答辩'");
            PageUtils.clearPage();
            List<PublicDataApi> fullList = publicDataApiMapper.selectList(wrapper);

            // 内存分页处理
            List<PublicDataApi> list = OtherUtil.memoryPage(fullList, domain.getPageSize(), domain.getPageNum());

            PublicApiVO vo = new PublicApiVO();
            vo.setPublicList(list);
            Map<String, List<PublicDataApi>> projectTeachersMap = fullList.stream()
                    .collect(Collectors.groupingBy(PublicDataApi::getMeetingStatus));
            projectTeachersMap.forEach((key, value) ->
                    builder.append(String.format("%s的活动有%d个；", key, value.size()))
            );
            vo.setActiveType(builder.toString());
            return tableSuccess2(vo.getPublicList(), fullList.size(), vo.getActiveType());
        } catch (Exception e) {
            log.error("---接口错误：" + e.getMessage());
            return OtherUtil.tableError("接口错误，请联系管理员");
        }
    }

    public static TableDataInfo tableSuccess2(List<?> list, long 原始的总数, String activeType) {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg(activeType);
        rspData.setRows(list);
        rspData.setTotal(原始的总数);
        rspData.setPageNum(new PageInfo(list).getPageNum());
        rspData.setPageSize(new PageInfo(list).getPageSize());
        return rspData;
    }
}
